<template>
  <div>
    <el-card>
      <!-- //面包屑导航 -->

      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>文章管理</el-breadcrumb-item>
        <el-breadcrumb-item>文章分类</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 行el -row  type设置布局模式
     列 el -col
 -->
      <el-row type="flex" justify="end">
        <el-col :span="3">
          <el-button type="primary" size="medium" @click="dialogVisible = true"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- {{ cateList }} -->
      <!-- 文章分类的表格 -->
      <!-- //stripe 隔行变色
border 加边框
prop 字段名
 -->
      <el-table :data="cateList" style="width: 100%" stripe border>
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <!-- //操作 -->
        <el-table-column label="操作">
          <!-- //上面有个obj.row每一行的数据 -->
          <template v-slot:default="{ row }">
            <el-button
              size="mini"
              sype="primary"
              @click="editDialogShow(row.id)"
              >修改</el-button
            >
            <el-button size="mini" type="danger" @click="delArtCate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 、、添加提示 -->
      <el-dialog
        title="添加分类"
        :visible.sync="dialogVisible"
        width="30%"
        @close="handleClose"
      >
        <!-- //放修改的的话框 -->
        <!-- 添加的表单 -->
        <el-form
          :model="addForm"
          :rules="addRules"
          ref="addRef"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="addForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <template #footer class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addArtCate">确 定</el-button>
        </template>
      </el-dialog>
      <!-- //修改分类的对话框 -->
      <el-dialog title="添加分类" :visible.sync="editDialogVisble" width="30%">
        <!-- //放表单 -->
        <!-- 添加的表单 -->
        <el-form
          :model="aditForm"
          :rules="addRules"
          ref="editRef"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="aditForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="aditForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <template #footer class="dialog-footer">
          <el-button @click="editDialogVisble = false">取 消</el-button>
          <el-button type="primary" @click="editSubmit">确 定</el-button>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
// reqDelArtCate
import { reqAddArtCate, reqDelArtCate, reqEditCateInfo } from '@/api/article'
// reqGetArtCateList
import { mapActions, mapState } from 'vuex'

export default {
  name: 'cate',
  data () {
    return {
      dialogVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 编辑表单的数据
      aditForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          {
            required: true,
            message: '分类名称不能为空',
            trigger: ['change', 'blur']
          },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: ['change', 'blur']
          }
        ],
        cate_alias: [
          {
            required: true,
            message: '分类别名不能为空',
            trigger: ['change', 'blur']
          },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: ['change', 'blur']
          }
        ]
      },
      // 控制编辑分类对话框
      editDialogVisble: false
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    ...mapActions('article', ['getCateList']),
    handleClose () {
      // 关闭对话框
      this.dialogVisible = false
      // 清空表单,切移除表单校验
      this.$refs.addRef.resetFields()
    },
    // 整体表单校验失败
    addArtCate () {
      this.$refs.addRef.validate(async (flag) => {
        if (!flag) return console.log('校验失败')
        const { data } = await reqAddArtCate(this.addForm)
        console.log(data)
        // 提示用户
        if (data.code !== 0) return this.$message.error('新增失败')
        this.$message.success(data.message)
        // 关闭对话框
        this.dialogVisible = false
        // 重新获取数据
        this.getCateList()
      })
    },
    // 删除文章分类
    delArtCate (id) {
      console.log('删除')
      if (id === 1 || id === 2) {
        return this.$message.error('禁止删除')
      }
      this.$confirm('此操作将删除文章分类, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          // 确认过了a
          // 发请求
          const { data } = await reqDelArtCate(id)
          if (data.code !== 0) return this.$message.error('删除失败')
          // 提示永华
          this.$message.success(data.message)
          // 数据回显
          this.editForm = data.data
          // 重新获取数据
          this.getCateList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 修改我们的对话框
    editDialogShow (id) {
      this.editDialogVisble = true
      console.log(id)
    },
    editSubmit () {
      this.$refs.editRef.validate(async (flag) => {
        if (!flag) return console.log('校验失败')
        const { data } = await reqEditCateInfo(this.editForm)
        // console.log(res)
        if (data.code !== 0) return this.$message.error(data.message)
        this.$message.success(data.message)
        // gu关闭对话框重新获取数据
        this.editDialogVisble = false
        this.getCateList()
      })
    }
  },
  computed: {
    ...mapState('article', ['cateList'])
  }
}
</script>

<style lang="less" scoped></style>
